package org.komarichin.jpa.dao;

import org.komarichin.jpa.entity.INews;
import org.komarichin.jpa.entity.News;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * User: Michael Komarichin
 * Date: 28.09.2009
 * Time: 12:00:25
 */
@Repository
@Transactional(readOnly = true)
public class NewsDAO extends JpaDaoSupport implements INewsDAO {
    private static final Logger log = LoggerFactory.getLogger(NewsDAO.class);

    /**
     * {@inheritDoc}
     */
    public INews getNewsById(Long id) {
        log.debug("getNewsById#id : {}", id);
        return getJpaTemplate().find(News.class, id);
    }

    /**
     * {@inheritDoc}
     *
     * @Deprecated not use this method, becoth instanceing object must do for example spring object factory
     */
    public INews createEmptyNews() {
        log.debug("createNews");
        return new News();
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public List<INews> getAll() {
        log.debug("getAll");
        return getJpaTemplate().findByNamedQuery("news.findAll");
    }

    /**
     * {@inheritDoc}
     */
    @Transactional(readOnly = false)
    public void save(INews object) {
        log.debug("save#object : {}", object);
        getJpaTemplate().persist(object);
    }

    /**
     * {@inheritDoc}
     */
    @Transactional(readOnly = false)
    public void update(INews object) {
        log.debug("update#object : {}", object);
        getJpaTemplate().merge(object);
    }

    /**
     * {@inheritDoc}
     */
    @Transactional(readOnly = false)
    public void saveOrUpdate(INews object) {
        log.debug("saveOrUpdate#object : {}", object);
        getJpaTemplate().merge(object);
    }

    /**
     * {@inheritDoc}
     */
    @Transactional(readOnly = false)
    public void delete(INews object) {
        log.debug("delete#object : {}", object);
        getJpaTemplate().merge(object);
    }

}
